package com.zerui.manager.icc.service;

import com.alibaba.fastjson.JSONObject;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.zerui.manager.icc.domain.DeptInfo;
import com.zerui.manager.icc.domain.DeptType;
import com.zerui.manager.icc.dto.DeptInfoVO;
import com.zerui.manager.icc.dto.DeptQueryVO;
import com.zerui.manager.icc.dto.DeptTreeVO;
import com.zerui.manager.icc.dto.PageVO;
import com.zerui.manager.icc.properties.ICCProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("iccDeptInfoService")
public class DeptInfoService extends AbstractBase {

    @Resource
    private ICCProperties iccProperties;

    /**
     * 生成全局唯一ID
     *
     * @return
     * @throws ClientException
     */
    public Long generateID() throws ClientException {
        JSONObject joData = this.generalRequest(this.iccProperties.getDepart().getSingleGenerateIdUrl(), Method.GET, null, "data");
        return joData.getLong("id");
    }

    /**
     * 新增部门信息
     *
     * @param deptInfo 部门信息
     * @return
     */
    public DeptInfo add(DeptInfo deptInfo) throws ClientException {
        Assert.notNull(deptInfo.getParentId(), "上级部门ID不能为空");
        Assert.notNull(deptInfo.getName(), "部门名称不能为空");
        if (null == deptInfo.getId()) deptInfo.setId(this.generateID());// 设置主键
        this.generalRequest(this.iccProperties.getDepart().getAddUrl(), Method.POST, deptInfo, "data");
        return deptInfo;
    }

    /**
     * 更新部门信息
     *
     * @param deptInfo 部门信息
     * @return
     */
    public Boolean update(DeptInfo deptInfo) throws ClientException {
        Assert.notNull(deptInfo.getId(), "部门ID不能为空");
        Assert.notNull(deptInfo.getParentId(), "上级部门ID不能为空");
        Assert.notNull(deptInfo.getName(), "部门名称不能为空");
        JSONObject joData = this.generalRequest(this.iccProperties.getDepart().getUpdateUrl(), Method.PUT, deptInfo, "data");
        return joData.containsKey("id");
    }

    /**
     * 删除指定ID的部门信息
     *
     * @param id 部门ID
     * @return
     */
    public Boolean delete(Long id) throws ClientException {
        Assert.notNull(id, "部门ID不能为空");
        Map<String, Object> joParam = new HashMap<>();
        joParam.put("id", id);
        JSONObject joData = this.generalRequest(this.iccProperties.getDepart().getDeleteUrl(), Method.DELETE, joParam, "data");
        return joData.containsKey("id");
    }

    /**
     * 选择指定主键的部门信息详情
     *
     * @param id 部门ID
     * @return
     */
    public DeptInfoVO selectById(Long id) throws ClientException {
        Assert.notNull(id, "部门ID不能为空");
        Map<String, String> joParam = new HashMap<>();
        joParam.put("id", String.valueOf(id));
        JSONObject joData = this.generalRequest(this.renderUrl(this.iccProperties.getDepart().getDetailUrl(), joParam), Method.GET, null, "data");
        List<DeptInfoVO> deptInfos = this.convert2TypeData(joData, DeptInfoVO.class);
        return deptInfos.size() > 0 ? deptInfos.get(0) : null;
    }

    /**
     * 部门分页查询列表
     *
     * @param queryVO 上级部门ID
     * @return
     */
    public PageVO<DeptInfoVO> findPage(DeptQueryVO queryVO) throws ClientException {
        Map<String, Object> joParam = new HashMap<>();
        joParam.put("parentId", queryVO.getParentId());
        joParam.put("containerLower", queryVO.getContainerLower());
        joParam.put("departmentIds", queryVO.getDepartmentIds());
        joParam.put("departmentSns", queryVO.getDepartmentSns());
        joParam.put("searchKey", queryVO.getSearchKey());
        joParam.put("pageNum", queryVO.getPageNum());
        joParam.put("pageSize", queryVO.getPageSize());
        JSONObject joData = this.generalRequest(iccProperties.getDepart().getPageUrl(), Method.POST, joParam, "data");
        PageVO<DeptInfoVO> pageVO = new PageVO<>();
        pageVO.setCurrentPage(joData.getInteger("currentPage"));
        pageVO.setPageSize(joData.getInteger("pageSize"));
        pageVO.setTotalPage(joData.getInteger("totalPage"));
        pageVO.setTotalRows(joData.getInteger("totalRows"));
        pageVO.setPageData(this.convert2TypeData(joData.getJSONArray("pageData"), DeptInfoVO.class));
        return pageVO;
    }

    /**
     * 查询部门树列表
     *
     * @param treeVO
     * @return
     */
    public List<DeptInfoVO> findTree(DeptTreeVO treeVO) throws ClientException {
        Map<String, Object> joParam = new HashMap<>();
        joParam.put("parentId", treeVO.getParentId());
        joParam.put("checkStat", treeVO.getCheckStat());
        joParam.put("name", treeVO.getName());
        joParam.put("showPersonNode", treeVO.getShowPersonNode());
        joParam.put("showCascadeNode", treeVO.getShowCascadeNode());
        joParam.put("personIdentityId", treeVO.getPersonIdentityId());
        joParam.put("pageNum", treeVO.getPageNum());
        joParam.put("needCheckDepartmentIdList", treeVO.getNeedCheckDepartmentIdList());
        JSONObject joData = this.generalRequest(iccProperties.getDepart().getTreeUrl(), Method.POST, joParam, "data");
        return this.convert2TypeData(joData.getJSONArray("value"), DeptInfoVO.class);
    }

    /**
     * 获取部门类型列表
     *
     * @return
     * @throws ClientException
     */
    public List<DeptType> getDeptTypes() throws ClientException {
        Map<String, String> joParam = new HashMap<>();
        joParam.put("stat", "1");
        JSONObject joData = this.generalRequest(this.renderUrl(this.iccProperties.getDepart().getDeptTypeUrl(), joParam), Method.GET, null, "data");
        return this.convert2TypeData(joData.getJSONArray("depTypes"), DeptType.class);
    }
}
